package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.query.EmployeeQueryObject;
import cn.wolfcode.education.service.IEmployeeService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@RequestMapping("employee")
@Controller
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    @RequestMapping("")
    @PermissionName("员工管理")
    @RequiresPermissions("employee:view")
    public String index() {
        return "employee";
    }

    @RequestMapping("list")
    @ResponseBody
    @PermissionName("员工列表")
    @RequiresPermissions("employee:list")
    public PageResult list(EmployeeQueryObject qo) {

        return employeeService.list(qo);
    }

    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("员工删除")
    @RequiresPermissions("employee:delete")
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            result.setMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    @PermissionName("员工保存")
    @RequiresPermissions("employee:save")
    public JSONResult save(Employee employee){
        JSONResult result = new JSONResult();
        try{
        employeeService.save(employee);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("保存失败");
        }
        return result;
    }

    @RequestMapping("edit")
    @ResponseBody
    @PermissionName("员工更新")
    @RequiresPermissions("employee:edit")
    public JSONResult edit(Employee employee){
        JSONResult result = new JSONResult();
        try{
            employeeService.edit(employee);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("更新失败");
        }
        return result;
    }

    @RequestMapping("export")
    @PermissionName("导出")
    @RequiresPermissions("employee:export")
    public void export(EmployeeQueryObject qo, HttpServletResponse response) throws Exception {
        //1.查询出列表数据
        qo.setRows(10000);
        List emps = employeeService.queryForList(qo);

        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标为outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //处理标题
        //创建一个可以写的sheet
        WritableSheet sheet = workbook.createSheet("employee",0);

        //创建一个单元格
        Label usernameLabel = new Label(0,0,"用户名");
        Label realNameLabel = new Label(1,0,"姓名");
        Label telLabel = new Label(2,0,"电话");
        Label emailLabel = new Label(3,0,"邮箱");
        Label deptLabel = new Label(4,0,"部门");
        Label inputTimeLabel = new Label(5,0,"录入时间");
        Label stateLabel = new Label(6,0,"状态");
        Label adminLabel = new Label(7,0,"管理员");

        sheet.addCell(usernameLabel);
        sheet.addCell(realNameLabel);
        sheet.addCell(telLabel);
        sheet.addCell(emailLabel);
        sheet.addCell(deptLabel);
        sheet.addCell(inputTimeLabel);
        sheet.addCell(stateLabel);
        sheet.addCell(adminLabel);

        //循环迭代list
        for (int i = 0;i < emps.size();i ++){
            Employee emp = (Employee) emps.get(i);

            int row = i + 1;

            Label tempusernameLabel = new Label(0, row,emp.getUsername());
            Label temprealNameLabel = new Label(1, row,emp.getRealName());
            Label temptelLabel = new Label(2, row,emp.getTel());
            Label tempemailLabel = new Label(3, row,emp.getEmail());
            Label tempdeptLabel = new Label(4, row,emp.getDept().getName());
            Label tempinputTimeLabel = new Label(5, row,emp.getInputTime().toLocaleString());

            String stateString = emp.getState() == Employee.STATE_NORMAL ? "正常" : "离职";
            Label tempstateLabel = new Label(6,row,stateString);

            String adminString = emp.isAdmin() ? "是" : "否";
            Label tempadminLabel = new Label(7,row,adminString);

            sheet.addCell(tempusernameLabel);
            sheet.addCell(temprealNameLabel);
            sheet.addCell(temptelLabel);
            sheet.addCell(tempemailLabel);
            sheet.addCell(tempdeptLabel);
            sheet.addCell(tempinputTimeLabel);
            sheet.addCell(tempstateLabel);
            sheet.addCell(tempadminLabel);

        }

        workbook.write();
        workbook.close();
    }

    //根据角色的id查询教师
    @RequestMapping("selectTeacherByRoleIdWithSchedle")
    @ResponseBody
    public List<Employee> selectTeacherByRoleIdWithSchedle(Long id){
        return employeeService.selectTeacherByRoleIdWithSchedle(id);
    }
    @RequestMapping("queryAll")
    @ResponseBody
    public List<Employee> queryAll() {
        return employeeService.queryAll();
    }
    /**
     * 根据当前用户查找部门下的员工
     */
    @RequestMapping("selectEmployeeByDeptId")
    @ResponseBody
    public List<Employee> selectEmployeeByDeptId(){
        return employeeService.selectEmployeeByDeptId();
    }


    @RequestMapping("queryEmpName")
    @ResponseBody
    public List<Employee> queryEmpName() {
        return employeeService.queryEmpName();
    }
}
